package ar.edu.unrn.lia.bean;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceException;

import org.primefaces.event.SelectEvent;

import ar.edu.unrn.lia.bean.datamodel.LazyTasaInteresDataModel;
import ar.edu.unrn.lia.model.TasaInteres;
import ar.edu.unrn.lia.service.TasaInteresService;

@ManagedBean(name = "tasaInteresBean")
@ViewScoped
public class TasaInteresBean extends GenericBean<TasaInteres> implements
		Serializable {

	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{tasaInteresService}")
	TasaInteresService entityService;

	public String create() {
		setEntity(new TasaInteres());
		return REDIRECT_CREATE;
	}

	public void onRowSelect(SelectEvent event) {
		agregarMensaje(FacesMessage.SEVERITY_INFO,
				"TasaInteres Seleccionado: ", ((TasaInteres) event.getObject())
						.getTasa().toString());
	}

	public String update() {
		try {

			if (getEntity().getId() == null) {
				entityService.save(getEntity());
				mensajeFlash("Crear TasaInteres",
						"Operación realizada con exito.");
			} else {
				entityService.update(getEntity());
				mensajeFlash("Modificar TasaInteres",
						"Operación realizada con exito.");
			}

			return REDIRECT_SEARCH;
		} catch (OptimisticLockException e) {
			agregarMensaje(FacesMessage.SEVERITY_ERROR, "Bloqueo",
					"Alquien modificó este TasaInteres, reconsulte.");
			return null;
		} catch (Exception e) {
			agregarMensaje(FacesMessage.SEVERITY_ERROR, "Error", e.getMessage());
			return null;
		}
	}

	public String delete(TasaInteres entity) {
		try {
			entityService.delete(entity);
			mensajeFlash("Eliminar TasaInteres",
					"Operación realizada con exito.");
			return REDIRECT_SEARCH;
		} catch (OptimisticLockException e) {
			agregarMensaje(FacesMessage.SEVERITY_ERROR, "Bloqueo",
					"Alquien modificó este TasaInteres, reconsulte.");
			return null;
		} catch (PersistenceException e) {
			agregarMensaje(FacesMessage.SEVERITY_ERROR, "Eliminar TasaInteres",
					"No se puede eliminar. El TasaInteres esta asociada a una propiedad.");
			return null;
		} catch (Exception e) {
			agregarMensaje(FacesMessage.SEVERITY_ERROR, "Eliminar TasaInteres",
					e.getMessage());
			return null;
		}
	}

	@PostConstruct
	public void init() {
		setModelLazy(new LazyTasaInteresDataModel(entityService));
	}

	public void inicio() {
		if (!FacesContext.getCurrentInstance().isPostback()) {
			if (getId() != null) {
				setEntity(entityService.getEntityById(getId()));

			} else {
				setEntity(new TasaInteres());
			}
		}
	}

	public TasaInteresService getEntityService() {
		return entityService;
	}

	public void setEntityService(TasaInteresService entityService) {
		this.entityService = entityService;
	}

}
